import RockingBar from "../reserve/RockingBar";

const {ccclass, property} = cc._decorator;

// 贪吃蛇 蛇
@ccclass
export default class Snake extends cc.Component {


    // 蛇移动一次的距离
    speed: number = 100; //步长

    // 备用状态
    up: number = 1;   // 向上的状态
    down: number = -1; // 向下的状态
    left: number = 2; // 向左的状态
    right: number = -2; // 向右的状态

    // 当前的状态
    state: number = 0;
    // 计时的变量
    timer: number = 0;
    // 蛇移动一次的间隔时长
    sudu: number = 30;
    // 是否在运行中，或者是否死亡
    running: boolean = false;
    // 分数
    score: number = 0;

    // 显示分数的节点
    @property(cc.Label)
    scoreLabel: cc.Label = null;
    // 蛇的身体存放的数组
    snakeBody: cc.Node[] = [];
    // 预制的蛇身体节点
    @property(cc.Prefab)
    prefabBody: cc.Prefab = null;
    // 重新开始的面板节点
    @property(cc.Node)
    resetLabel: cc.Node = null;
    // onLoad () {}
    @property(RockingBar)
    Rocker: RockingBar = null;
    // speed:number =100;
    sx: number = 0;
    sy: number = 1;

    start() {
        cc.director.getCollisionManager().enabled = true; // 开启碰撞检测 全局的，在程序运行时任何一个地方开启即可
        this.reset();
    }

    // 重新开始游戏
    reset() {
        this.node.setPosition(new cc.Vec2(0, 0));
        this.score = 0;
        this.snakeBody = [];
        this.resetLabel.active = false;
        this.running = true;
    }

 jiaodu:number = 0;
    // 更新每贞的画面
    update(dt) {
        this.timer++;
        //移动
        if(this.running){
            this.node.x += this.sx;
            this.node.y += this.sy;
            this.limitng();
                // let x = this.node.x, y = this.node.y;

                // this.node.children[0].x
            let worldPos = this.node.children[0].convertToWorldSpaceAR(this.node.children[0].position);
            console.log("世界坐标"+ worldPos.x+"," + worldPos.y);
            let canPos = this.node.parent.convertToNodeSpaceAR(worldPos);
            console.log("局部坐标"+ canPos.x+"," + canPos.y);
            console.log("玩家坐标"+ this.node.x +"," + this.node.y);
            let x = canPos.x, y = canPos.y;
                this.nextBody(x, y,dt);

            this.scoreLabel.string = this.score.toString();

            if (this.Rocker.dir.mag() > 0.5) {
                let vx = this.Rocker.dir.x * this.speed;
                let vy = this.Rocker.dir.y * this.speed;

                this.sx = vx * dt;
                this.sy = vy * dt;

                //方向计算
                let r = Math.atan2(this.Rocker.dir.y, this.Rocker.dir.x);
                let degree = r * 180 / (Math.PI);
                degree = 360 - degree + 90;
                this.node.rotation = degree;
                // this.jiaodu = degree;
                // if(this.node.rotation > this.jiaodu){
                //     this.node.rotation-= 5;
                // }else{
                //     this.node.rotation+= 5;
                // }
            }
        }

    }

    // 蛇身体跟随蛇头
    nextBody(x: number, y: number, dt:number) {
        // if (this.timer % 5 == 0) {
        //     let old = new cc.Vec2(x, y), oldp;
        //     for (let i = 0, j = this.snakeBody.length; i < j; i++) {
        //         oldp = this.snakeBody[i].getPosition();
        //         this.snakeBody[i].setPosition(old);
        //         old = oldp;
        //     }
        // }
        let pos = cc.v2(x,y);

        // let len = pos.mag();
        // let sx = pos.x / len * this.speed * dt;
        // let sy = pos.y / len * this.speed * dt;
        for (let i = 0, j = this.snakeBody.length; i < j; i++) {
            let newPos = pos.subSelf(this.snakeBody[i].getPosition());
            pos = this.snakeBody[i].getPosition();
            let len = newPos.mag();
            let sx = newPos.x / len * this.speed * dt;
            let sy = newPos.y / len * this.speed * dt;
            this.snakeBody[i].x += sx;
            this.snakeBody[i].y += sy;
            let r = Math.atan2(newPos.y, newPos.x);
            let degree = r * 180 / (Math.PI);
            degree = 360 - degree + 90;
            this.snakeBody[i].rotation = degree;

            // len = pos.mag();
            // sx = pos.x / len * this.speed * dt;
            // sy = pos.y / len * this.speed * dt;
        }


    }

    // 限制蛇头出界
    limitng() {
        let minX = -this.node.parent.width / 2 + this.node.width / 2; //最小X坐标；
        let maxX = Math.abs(minX);
        let minY = -this.node.parent.height / 2 + this.node.height / 2; //最小Y坐标；
        let maxY = Math.abs(minY);
        let nPos = this.node.getPosition(); //节点实时坐标；

        if (nPos.x < minX) {
            nPos.x = minX;
        }
        ;
        if (nPos.x > maxX) {
            nPos.x = maxX;
        }
        ;
        if (nPos.y < minY) {
            nPos.y = minY;
        }
        ;
        if (nPos.y > maxY) {
            nPos.y = maxY;
        }
        ;
        this.node.setPosition(nPos);
    }


    // 处理碰撞事件
    onCollisionEnter(other, self) {
        // 如果碰撞到墙壁
        if (other.node.group == "wall") {
            this.node.removeAllChildren();
            for (let i = 0, j = this.snakeBody.length; i < j; i++) {
                this.snakeBody[i].destroy();
            }
            this.running = false;
            this.resetLabel.active = true;

        }
        // 如果碰撞到食物
        if (other.node.group == "food") {
            this.score++;
            var loadobj = cc.instantiate(this.prefabBody);//将预制体克隆到场景
            this.node.parent.addChild(loadobj);//将克隆出的物体作为子物体
            // let vx = this.Rocker.dir.x * 10;
            // let vy = this.Rocker.dir.y * 10;

            // if(this.snakeBody.length == 0){
                loadobj.setPosition(this.node.getPosition());
            // }else{
            //     loadobj.setPosition(0, this.snakeBody.length +1 * -10);
            // }
            this.snakeBody[this.snakeBody.length] = loadobj;
        }
    }
}
